//
//  HomePageViewController.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/8/29.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "HomePageViewController.h"
#import "HP_LoanViewController.h"
#import "HP_InstalmentViewController.h"
#import "LoginAction.h"
#import "Mediator+Welcome.h"

@interface HomePageViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)HP_LoanViewController *loanVC;
@property(nonatomic,strong)HP_InstalmentViewController *instalmentVC;
@property(nonatomic,strong)UIButton *loanBtn;
@property(nonatomic,strong)UIView *headerLine;
@property(nonatomic,strong)UIButton *instalmentBtn;
@property(nonatomic,strong)UIView *headTitltView;
@property(nonatomic,strong)UIScrollView *myScrollView;
@property(nonatomic,strong)UIImageView *redImageView;





@end

@implementation HomePageViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
 
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
    [[Mediator sharedInstance] HFQGuidePageChoiceAppear];
    // Do any additional setup after loading the view.
}

-(void)initViews{
    
    if ([[Mediator sharedInstance] Login_IsShowFourModule]) {
        self.navigationItem.titleView = self.headTitltView;
        self.title = @"首页";
        [self.view addSubview:self.myScrollView];
    }else {
        self.instalmentVC = [[HP_InstalmentViewController alloc]init];
        [self addChildViewController:self.instalmentVC];
        [self.instalmentVC didMoveToParentViewController:self];
        [self.view addSubview:self.instalmentVC.view];
    }
    
    [self initNavigationBar];
    
    
}



- (void)initNavigationBar{
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    
    
    UIImageView *messageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 24, 24)];
    messageView.image = [UIImage imageNamed:@"my_nav_ic_message copy"];
    
    self.redImageView = [[UIImageView alloc]initWithFrame:CGRectMake(18, -2, 9, 9)];
    self.redImageView.image = [UIImage imageNamed:@"home_nav_ic_hongdian"];
    messageView.userInteractionEnabled = YES;
    [messageView addGestureRecognizer:tap];
    [messageView addSubview:self.redImageView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:messageView];
    
    
    
}
-(void)tapAction{
    UIViewController *messageVC = [[Mediator sharedInstance] My_MessageViewController];
    [self.navigationController pushViewController:messageVC animated:YES];}

#pragma mark --------scrollViewDelegate--------------
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x == 0) {
        [UIView animateWithDuration:0.3 animations:^{
            self.loanBtn.selected = NO;
            self.instalmentBtn.selected = YES;
            self.headerLine.frame = CGRectMake(0, 28, 40, 2);
            self.headerLine.backgroundColor = HexColor(0xFF7A45);
        }];
        
    }else if (scrollView.contentOffset.x == ScreenWidthMao){
        [UIView animateWithDuration:0.3 animations:^{
            self.loanBtn.selected = YES;
            self.instalmentBtn.selected = NO;
            self.headerLine.frame = CGRectMake(70 , 28, 40, 2);
            self.headerLine.backgroundColor = HexColor(0xBA915E);
        }];
        
    }else{
        self.headerLine.frame = CGRectMake(70.f*scrollView.contentOffset.x/ScreenWidthMao , 28, 40, 2);
    }
}
-(void)instalmentBtnAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.loanBtn.selected = NO;
        self.instalmentBtn.selected = YES;
        self.headerLine.frame = CGRectMake(0, 28, 40, 2);
        self.headerLine.backgroundColor = HexColor(0xFF7A45);
        self.myScrollView.contentOffset = CGPointMake(0, 0);
    }];
}

-(void)loanBtnAction{

    
    [UIView animateWithDuration:0.3 animations:^{
        self.loanBtn.selected = YES;
        self.instalmentBtn.selected = NO;
        self.headerLine.frame = CGRectMake(70 , 28, 40, 2);
        self.headerLine.backgroundColor = HexColor(0xBA915E);
        self.myScrollView.contentOffset = CGPointMake(ScreenWidthMao, 0);
        
    }];
    
}

-(UIScrollView *)myScrollView{
    if (!_myScrollView) {
        _myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidthMao, self.view.frame.size.height)];
        _myScrollView.contentSize = CGSizeMake(ScreenWidthMao*2, 0);
        _myScrollView.bounces = NO;
        _myScrollView.pagingEnabled = YES;
        _myScrollView.delegate = self;
        _myScrollView.showsVerticalScrollIndicator = NO;
        _myScrollView.showsHorizontalScrollIndicator = NO;
        
        self.instalmentVC = [[HP_InstalmentViewController alloc]init];
//        [self addChildViewController:self.instalmentVC];
//        [self.instalmentVC didMoveToParentViewController:self];
        self.instalmentVC.view.frame = CGRectMake(0, 0, ScreenWidthMao, self.view.frame.size.height);
        [_myScrollView addSubview:self.instalmentVC.view];
        self.loanVC = [[HP_LoanViewController alloc]init];
//        [self addChildViewController:self.loanVC];
//        [self.loanVC didMoveToParentViewController:self];
        self.loanVC.view.frame = CGRectMake(ScreenWidthMao, 0, ScreenWidthMao, self.view.frame.size.height);
        [_myScrollView addSubview:self.loanVC.view];
    
    }
    return _myScrollView;
}
-(UIView *)headTitltView{
    if (!_headTitltView) {
        _headTitltView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 115, 30)];
        _instalmentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_instalmentBtn setTitle:@"分期" forState:UIControlStateNormal];
        _instalmentBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [_instalmentBtn setTitleColor:HexColor(0xFF7A45) forState:UIControlStateSelected];
        [_instalmentBtn setTitleColor:ThemeMaoBlackColor forState:UIControlStateNormal];
        _instalmentBtn.frame = CGRectMake(0, 0, 40, 30);
        [_instalmentBtn addTarget:self action:@selector(instalmentBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_headTitltView addSubview:_instalmentBtn];
        
        _headerLine = [[UIView alloc]init];
        _headerLine.backgroundColor = HexColor(0xFF7A45);
        _headerLine.frame = CGRectMake(0, 28, 40, 2);
        [_headTitltView addSubview:_headerLine];
        
        _loanBtn= [UIButton buttonWithType:UIButtonTypeCustom];
        [_loanBtn setTitle:@"借贷" forState:UIControlStateNormal];
        _loanBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [_loanBtn setTitleColor:HexColor(0xBA915E) forState:UIControlStateSelected];
        [_loanBtn setTitleColor:ThemeMaoBlackColor forState:UIControlStateNormal];
        _loanBtn.frame = CGRectMake(70, 0, 40, 30);
        [_loanBtn addTarget:self action:@selector(loanBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_headTitltView addSubview:_loanBtn];
        
        self.instalmentBtn.selected = YES;
        self.loanBtn.selected = NO;
        
    }
    return _headTitltView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
